// controller 里面导入 model ，调用model 的方法，查询数据库
import Error from '../modles/errorModelSchema';
import Performance from '../modles/performanceModelSchema';
import Resource from '../modles/resourceModelSchema';
import logger from '../logger';
import url from 'url';

interface TraceType {
  insertTrace: Function;
  saveErrors: Function;
  saveRessource: Function;
  savePerformance: Function;
  getErrorList: Function;
}
const TraceCtrl = <TraceType>{};

// // 获取全部用户信息
// TraceCtrl.insertTrace = (req: any, res: any, next: Function) => {
//   const queryData = url.parse(req.url, true)?.query || {};
//   const { performance, resources, errors }: any = queryData;
//   // console.log(JSON.parse(queryData));
//   const traceData = {
//     performance: JSON.parse(performance),
//     resources: JSON.parse(resources),
//     errors: JSON.parse(errors),
//   };

//   // 分开处理 errors performance  resources 便于后端取值分析数据
//   Trace.create(traceData).then((trace) => {
//     res.send({
//       success: true,
//     });
//   });
// };

// 保存errors
TraceCtrl.saveErrors = (req: any, res: any, next: Function) => {
  const queryData = url.parse(req.url, true)?.query || {};
  const { errors }: any = queryData;
  Error.create({ errors }).then((trace) => {
    res.send({
      success: true,
    });
  });
};

// 保存 resource
TraceCtrl.saveRessource = (req: any, res: any, next: Function) => {
  const queryData = url.parse(req.url, true)?.query || {};
  const { resources }: any = queryData;
  Resource.create({ resources }).then((trace) => {
    res.send({
      success: true,
    });
  });
};
// 保存页面 performance
TraceCtrl.savePerformance = (req: any, res: any, next: Function) => {
  const queryData = url.parse(req.url, true)?.query || {};
  const { performance }: any = queryData;
  Performance.create({ performance }).then((trace) => {
    res.send({
      success: true,
    });
  });
};

export default TraceCtrl;
